function PagingBar(element) {
	this._container = element;

	this._templates = {
		pagingBar: "<div class='paging-bar'></div>",
		
		pagingTextCon:"<div class='text-con'></div>",
		pagingText1: "<span>每页</span>",
		pagingSize:"<select name=''><option value='5'>5</option><option value='10'>10</option><option value='15'>15</option></select>",
		pagingText2: "<span>行，共  </span>",
		pagingTotal: "<span></span>",
		pagingText3: "<span> 行</span>",
		
		pagingUl: "<ul></ul>",
		pagingFirst: "<li class='first'>首页</li>",
		pagingPrevious: "<li class='previous'>上一页</li>",
		pagingMore: "<li class='more btn-disabled'>...</li>",
		pagingNum: "<li class='num'></li>",
		pagingNext: "<li class='next'>下一页</li>",
		pagingLast: "<li class='last'>尾页</li>",
	}


	this.callbackList=[]

}

var _proto = PagingBar.prototype;

/*
 "pagination": {
		"endRow": 1,
		"firstPage": 1,
		"hasNextPage": false,
		"hasPreviousPage": false,
		"isFirstPage": true,
		"isLastPage": true,
		"lastPage": 1,
		"navigatePages": 8,
		"navigatepageNums": [
			1
		],
		"nextPage": 0,
		"pageNum": 1,
		"pageSize": 5,
		"pages": 1,
		"prePage": 0,
		"size": 1,
		"startRow": 1,
		"total": 1
	}
 * */

/*初始化*/
_proto.init = function(pagination) {
	
		if(pagination.total==0){
			this._container.hide();
			return;
		}else{
			this._container.show();
		}
		var _self=this;
		function _turnPage(pageNum,pageSize){

			for(index in _self.callbackList){
				_self.callbackList[index](pageNum,pageSize);
			}
		}
		this._container.html("");
		$pagingUl=$(this._templates["pagingUl"]);
		$pagingFirst=$(this._templates["pagingFirst"]);
		$pagingPrevious=$(this._templates["pagingPrevious"]);
		
		
		if(pagination.isFirstPage){
			$pagingFirst.addClass("btn-disabled");
			$pagingPrevious.addClass("btn-disabled");
		}else{
			$pagingFirst.click(function(){
			_turnPage(1,pagination.pageSize);
			});
			$pagingPrevious.click(function(){
				_turnPage(--pagination.pageNum>0?pagination.pageNum:pagination.firstPage,pagination.pageSize);
			});
		}
		
		
		$pagingUl.append($pagingFirst);
		$pagingUl.append($pagingPrevious);
		
		//当前分页所处页数
		var pageNum = pagination.pageNum;
		//
		var begin = pagination.pageNum-2<1?pagination.firstPage:pagination.pageNum-2;
		var end = pagination.pageNum+2>pagination.lastPage?pagination.lastPage:pagination.pageNum+2;

		if(begin==pagination.firstPage){
			end=pagination.firstPage+4<=pagination.lastPage?pagination.firstPage+4:pagination.lastPage;
		}
		if(end==pagination.lastPage){
			begin=pagination.lastPage-4>=pagination.firstPage?pagination.lastPage-4:pagination.firstPage;
		}
		
		if(begin!=pagination.firstPage){
			$pagingMore=$(this._templates["pagingMore"]);
			$pagingUl.append($pagingMore);
		}
		for(;begin<=end;begin++){
			$pageNum = $(this._templates["pagingNum"]);
			$pageNum.text(begin);
			if(begin==pagination.pageNum){
				$pageNum.addClass("btn-disabled");
			}else{
				(function(begin){
					$pageNum.click(function(){
					_turnPage(begin,pagination.pageSize);
					});
				})(begin);
				
			}
			$pagingUl.append($pageNum);
		}
		
		
		if(end!=pagination.lastPage){
			$pagingMore=$(this._templates["pagingMore"]);
			$pagingUl.append($pagingMore);
		}
		
		$pagingNext=$(this._templates["pagingNext"]);
		$pagingLast=$(this._templates["pagingLast"]);
		
		
		
		if(pagination.isLastPage){
			$pagingNext.addClass("btn-disabled");
			$pagingLast.addClass("btn-disabled");
		}else{
			$pagingLast.click(function(){
			_turnPage(pagination.pages,pagination.pageSize);
			});
			$pagingNext.click(function(){
				_turnPage(++pagination.pageNum<=pagination.lastPage?pagination.pageNum:pagination.lastPage,pagination.pageSize);
			});
		}
		
		
		$pagingUl.append($pagingNext);
		$pagingUl.append($pagingLast);
		
		$pagingBar=$(this._templates["pagingBar"]);
		
		
		$pagingTextCon=$(this._templates["pagingTextCon"]);
		$pagingText1=$(this._templates["pagingText1"]);
		$pagingSize=$(this._templates["pagingSize"]);
		$pagingText2=$(this._templates["pagingText2"]);
		$pagingTotal=$(this._templates["pagingTotal"]);
		$pagingText3=$(this._templates["pagingText3"]);
		

		$pagingTextCon.append($pagingText1);
		$pagingSize.val(pagination.pageSize);
		$pagingSize.change(function(){
			_turnPage(pagination.pageNum,Number($(this).val()));
			
		})
		$pagingTextCon.append($pagingSize);
		$pagingTextCon.append($pagingText2);
		$pagingTotal.text(pagination.total);
		$pagingTextCon.append($pagingTotal);
		$pagingTextCon.append($pagingText3);
		
		$pagingBar.append($pagingTextCon);
		$pagingBar.append($pagingUl);
		this._container.append($pagingBar);

	}


_proto.onChange= function(fun) {
	this.callbackList.push(fun);
}

$.fn.pagingBar = function() {
	return new PagingBar(this);
}